package club.monkeywood.util.wechat;

import club.monkeywood.config.wechat.WechatConfig;
import club.monkeywood.util.encrypt.SHA1Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;

@Component
public class CheckUtil {

//    AccountConfig accountConfig= new AccountConfig();

    @Autowired
    private WechatConfig af;
    private static WechatConfig wechatConfig;

    @PostConstruct
    private void beforeInit() {
        wechatConfig = af;
    }

    public static boolean checkSignature(String signature,String timestamp,String nonce){

        String[] arr = new String[]{wechatConfig.getToken(),timestamp,nonce};

//        排序
        Arrays.sort(arr);

//        裝配成字符串
        StringBuffer content = new StringBuffer();
        for(int i=0;i<arr.length;i++){
            content.append(arr[i]);
        }

//        加密字符串
        String temp = SHA1Util.getSha1(content.toString());

        return temp.equals(signature);
    }
}
